/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.directory.mavibot.btree;

/**
 * Contains a LRU cache to make possible synchronized access by multiple threads 
 *
 * @param <K> The Key type
 * @param <V> The Value type
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 */
public class SynchronyzedCache 
{
	/** correspond to the cache that we have to synchronized */
	private CacheLRU cache;
	
	/**
	 * create a new SynchronizedCache, which encapsulate a CacheLRU in a synchronizedMap  
	 * @param size
	 */
	public SynchronyzedCache( int size )
	{
		cache=new CacheLRU(size);
	}
	
	/**
	 * access to the value correspond to the parameter key
	 * @param key
	 * @return
	 */
	public synchronized Object get( Object key )
	{
		return cache.get( key );
	}
	
	/**
	 * put a new couple key value in the cache
	 * @param key
	 * @param value
	 * @return
	 */
	public synchronized Object put( Object key, Object value )
	{
		return cache.put( key, value );
	}
	
	/**
	 * call the method remove for each key of the CacheLRU
	 */
	public synchronized void removeAll()
	{
		for( Object k : cache.keySet() )
		{
			cache.remove( k );
		}
		cache.setClose(true);
	}
	
	public synchronized boolean isClose()
	{
		return cache.isClose();
	}
}
